Imports DAL
Imports MyClasses
Imports C1.Win.C1FlexGrid
Imports System.Configuration

Public Class frmSaleWithoutStock
    Public ItemID As Integer
    Public ItemName As String
    Public btResult As Byte

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim intResult As Integer
        Try
            If ValidateData() = 1 Then
                intResult = SaveData()
                If intResult <= 0 Then Exit Sub
                Me.Close()
            End If

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaleWithoutStock")
        End Try
    End Sub

    Function SaveData() As Integer
        'Kiran@ITS on 20071203:Pass the Item Id        
        Dim Item(0) As CPurchaseItem
        Dim intRes As Integer
        Dim intManufacurer As Integer
        Dim intResult As Integer
        Dim dtbVItems As New DataTable
        Try
            Item(0) = New CPurchaseItem
            With Item(0)
                .BatchNo = txtBatchNO.Text.ToString()
                .Quantity = txtQuantity.Text
                .StockQuantity = txtQuantity.Text
                .MRP = txtMRP.Text
                .ManufactureDate = dtpMfgDate.Value.Date.AddDays(-1)
                'Kiran@ITS on 20081103:Added for Expiry Date
                '.ExpiryDate = dtpExpDate.Value.Date
                .ExpiryDate = BCommon.GetDatewithLastDay(dtpExpDate.Value.Date)                
                .ItemID = ItemID
            End With
            Dim cPurInv As New CPurchaseInvoice
            With cPurInv
                .InvoiceID = 0
                .InvoiceSeqID = 0
                .VoucherDate = Date.Now().Date()
                .BillDate = Date.Now().Date()
                .VendorID = cmbVendor.SelectedValue()
                .DcID = 0
                .Remarks = ""
            End With

            dtbVItems = DItem.GetvendorItems(cmbVendor.SelectedValue, ItemID)
            If dtbVItems.Rows.Count <= 0 Then
                intManufacurer = DManufacturer.GetManufacturer(ItemID)
                intResult = DItem.SaveItemsMV(intManufacurer, cmbVendor.SelectedValue, ItemID)
                If intResult = 0 Then BCommon.ShowMsgError(4) : Exit Function
            End If

            'intRes = BSaleWithoutSale.SaveTempPurchase(Item(0), ItemID, cmbVendor.SelectedValue)

            'dtbTempItems = DPurchaseInvoice.GetTempPurItems(objBillItem(intCount).ItemID, objBillItem(intCount).BatchNo, cPurInv.VendorID)
            ''intQty = DPurchaseInvoice.GetTempPurItemQty(objBillItem(intCount).ItemID, objBillItem(intCount).BatchNo, cPurInv.VendorID)
            'If Not IsNothing(dtbTempItems) AndAlso dtbTempItems.Rows.Count > 0 Then
            '    int1 = DPurchaseInvoice.UpdatePurchaseStatus(dtbTempItems.Rows(0).Item(0))
            '    objBillItem(intCount).StockQuantity = .Item(intCount + 1, 9) - dtbTempItems.Rows(0).Item(4)
            'Else
            '    If objBillItem(intCount).SchemeID > 0 Then
            '        objBillItem(intCount).StockQuantity = Val(.Item(intCount + 1, 9))
            '    Else
            '        objBillItem(intCount).StockQuantity = Val(.Item(intCount + 1, 9)) + objBillItem(intCount).SchemeItemQty
            '    End If

            'End If
            '>>>>>>

            intRes = DPurchaseInvoice.saveDC("S", cPurInv, Item, "A", "TDC")
            'If intRes > 0 Then BCommon.ShowMsgError(3) Else BCommon.ShowMsgError(4)

            '<<<<<<

            If intRes <= 0 Then
                BCommon.ShowMsgError(4)
                btResult = 0
                Return 0
            Else
                btResult = 1
                BCommon.ShowMsgError(3)
                Return 1
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Return 0
        End Try
        'Return 1
    End Function

    Private Sub clear()
        txtBatchNO.Text = ""
        txtMRP.Text = ""
        dtpExpDate.Text = ""
        dtpMfgDate.Text = ""
        txtQuantity.Text = ""
        cmbVendor.SelectedIndex = 0
    End Sub

    Private Sub frmSaleWithoutStock_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Sale Without Stock")
        End If
    End Sub

    Private Sub frmSaleWithoutStock_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call BCommon.SetFormStandards(Me)
        Dim dtbvendor As DataTable
        Try
            dtbvendor = BVendor.GetVendor()
            BCommon.SetComboValues(cmbVendor, dtbvendor, "VendorID", "VendorName")
            dtpMfgDate.Value = Format(dtpMfgDate.Value, ConfigurationManager.AppSettings("DateFormat").ToString())
            dtpExpDate.Value = Format(dtpMfgDate.Value, ConfigurationManager.AppSettings("DateFormat").ToString())
            Call clear()

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaleWithoutStock")
        End Try
    End Sub
    Private Function ValidateData() As Byte
        Try
            If txtBatchNO.Text = "" Then
                BCommon.ShowMsgError(10262)
                txtBatchNO.Select()
                txtBatchNO.Focus()
                Return 0
            ElseIf txtMRP.Text = "" Then
                BCommon.ShowMsgError(10263)
                txtMRP.Select()
                txtMRP.Focus()
                Return 0
            ElseIf Not IsNumeric(txtMRP.Text) Then
                BCommon.ShowMsgError(10311)
                txtMRP.Select(0, txtMRP.TextLength)
                txtMRP.Focus()
                Return 0
            ElseIf CInt(txtMRP.Text) < 0 Then
                BCommon.ShowMsgError(10312)
                txtMRP.Select(0, txtMRP.TextLength)
                txtMRP.Focus()
                Return 0
            ElseIf txtQuantity.Text = "" Then
                BCommon.ShowMsgError(10264)
                txtQuantity.Select()
                txtQuantity.Focus()
                Return 0
            ElseIf Not IsNumeric(txtQuantity.Text) Then
                BCommon.ShowMsgError(10313)
                txtQuantity.Select(0, txtQuantity.TextLength)
                txtQuantity.Focus()
                Return 0
            ElseIf CInt(txtQuantity.Text) < 0 Then
                BCommon.ShowMsgError(10314)
                txtQuantity.Select(0, txtQuantity.TextLength)
                txtQuantity.Focus()
                Return 0
            ElseIf dtpMfgDate.Value >= Date.Today Then
                BCommon.ShowMsgError(10119)
                dtpMfgDate.Value = Now.Date().AddDays(-1)
                dtpMfgDate.Focus()
                Return 0
            ElseIf Date.Today > dtpExpDate.Value Then
                BCommon.ShowMsgError(10113)
                dtpExpDate.Value = BCommon.GetDatewithLastDay(Now.Date())
                dtpExpDate.Focus()
                Return 0
            ElseIf cmbVendor.SelectedIndex <= 0 Then
                BCommon.ShowMsgError(10261)
                cmbVendor.Focus()
                Return 0
            Else
                Return 1
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
        End Try
    End Function
    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub txtMRP_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMRP.KeyDown
        If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
        If BCommon.ValidateText(e.KeyCode, "NUM", False) = False Then
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub txtQuantity_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtQuantity.KeyDown
        If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
        If BCommon.ValidateText(e.KeyCode, "NUM", False) = False Then
            e.SuppressKeyPress = True
        End If
    End Sub

    Private Sub txtBatchNO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtBatchNO.KeyDown
        If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
    End Sub

    Private Sub dtpExpDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpExpDate.KeyDown
        If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
    End Sub

    Private Sub dtpMfgDate_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpMfgDate.KeyDown
        If e.KeyCode = Keys.Enter Then SendKeys.Send("{TAB}")
    End Sub
    Private Sub cmbVendor_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cmbVendor.KeyDown
        If e.KeyCode = Keys.Enter Then
            If cmbVendor.SelectedValue < 0 Then Exit Sub
            btnSave.Focus()
        End If
    End Sub

    Private Sub txtBatchNO_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBatchNO.LostFocus
        Try
            Dim dtbBatchDetail As DataTable

            dtbBatchDetail = DPurchaseInvoice.BatchDetail(ItemID, txtBatchNO.Text.ToString())
            If dtbBatchDetail.Rows.Count > 0 Then
                dtpMfgDate.Value = dtbBatchDetail.Rows(0).Item("MfgDate")
                dtpMfgDate.Enabled = False
                dtpExpDate.Value = dtbBatchDetail.Rows(0).Item("ExpDate")
                dtpExpDate.Enabled = False
                txtMRP.Text = dtbBatchDetail.Rows(0).Item("MRP")
            Else
                dtpMfgDate.Enabled = True
                dtpExpDate.Enabled = True
                'txtMRP.Text = String.Empty
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "lblBatchNo_LostFocus")
        End Try

    End Sub
End Class